package com.soft.mainsite.module.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.data.dao.DataMapper;
import com.soft.mainsite.module.data.dao.UserDataMapper;
import com.soft.mainsite.module.data.dto.DataQueryParams;
import com.soft.mainsite.module.data.dto.UserDataDTO;
import com.soft.mainsite.module.data.entity.MainData;
import com.soft.mainsite.module.data.service.DataService;
import com.soft.mainsite.module.data.viewmodel.CategoryVM;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/11/25 0:24
 * Stay hungry,stay foolish!
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class DataServiceImpl implements DataService {

    @Resource
    private DataMapper dataMapper;

    @Resource
    private UserDataMapper userDataMapper;

    @Override
    public PageInfo<MainData> pageDataByConditions(DataQueryParams queryParams, Integer pageNumber, Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);

        List<MainData> mainDataList = dataMapper.pageDataByConditions(queryParams);

        List<UserDataDTO> userDataList = new ArrayList<>();
        String userPhone = queryParams.getUserPhone();

        if (userPhone != null && !"".equals(userPhone)) {
            userDataList = userDataMapper.userDataList(userPhone, null);
        }

        // 过滤资料列表里的是否领取字段，如果为空，代表没有领取，给其赋值为0，是否收藏，如果为空，则赋值为0
        for (MainData mainData : mainDataList) {
            if (userDataList != null && userDataList.size() > 0) {
                for (UserDataDTO userDataDTO : userDataList) {
                    Integer dataId = userDataDTO.getDataId();
                    Integer isFavorite = userDataDTO.getIsFavorite();
                    Integer isReceived = userDataDTO.getIsReceived();
                    if (dataId.equals(mainData.getId())) {
                        mainData.setIsReceived(isReceived);
                        mainData.setIsFavorite(isFavorite);
                        mainData.setUserPhone(userPhone);
                    }
                }
            } else {
                mainData.setIsFavorite(0);
                mainData.setIsReceived(0);
                mainData.setUserPhone(userPhone);
            }
        }

        return new PageInfo<>(mainDataList);
    }

    @Override
    public String saveMainData(MainData mainData) {

        mainData.setCreateUser("root");
        mainData.setCreateTime(new Date());
        mainData.setIsDel(0);
        int insert = dataMapper.saveMainData(mainData);
        if (insert > 0) {
            log.info("E|DataServiceImpl|saveMainData()|保存文件资料成功！");
            return JsonUtil.toSuccess("操作成功","data-tab",false);
        }

        log.error("E|DataServiceImpl|saveMainData()|保存文件资料失败！");

        return JsonUtil.toError("操作失败");
    }

    @Override
    public String delete(Long[] ids) {

        if (ids != null && ids.length > 0) {
            int insert = dataMapper.deleteByIds(ids);
            if (insert > 0) {
                log.info("E|DataServiceImpl|delete()|删除文件资料成功！");
                return JsonUtil.toSuccess("操作成功","data-tab",false);
            }
        }

        return JsonUtil.toError("操作失败");
    }

    @Override
    public List<CategoryVM> getCategoryList(String categoryName, String userPhone) {

        log.info("根据学科名模糊查询学科列表接受到的参数 categoryName = {}, userPhone = {}", categoryName, userPhone);

        List<CategoryVM> categoryList = dataMapper.getCategoryList(categoryName);
        log.info("根据学科名模糊查询学科列表 categoryList = {}", categoryList);

        return categoryList;
    }

    @Override
    public List<MainData> userDataList(Integer categoryId, String userPhone) {

        log.info("根据分类id和用户手机号查询资料列表接受到的参数 categoryId = {}, userPhone = {}", categoryId, userPhone);

        // 查询到当前学科列表
        List<MainData> mainDataList = dataMapper.selectListByCategoryId(categoryId);

        // 再查询用户资料列表，做比较，把用户领取和收藏的赋值
        List<UserDataDTO> userDataList = userDataMapper.userDataList(userPhone, null);

        if (userDataList == null || userDataList.size() == 0) {
            log.info("根据用户手机号查询用户领取资料为空，返回该分类科目id下的所有科目资料 mainDataList = {}", mainDataList);
            return mainDataList;
        }

        for (UserDataDTO userDataDTO : userDataList) {
            Integer dataId = userDataDTO.getDataId();
            Integer isFavorite = userDataDTO.getIsFavorite();
            Integer isReceived = userDataDTO.getIsReceived();

            if (mainDataList != null && mainDataList.size() > 0) {
                for (MainData mainData : mainDataList) {
                    if (dataId.equals(mainData.getId())) {
                        mainData.setIsReceived(isReceived);
                        mainData.setIsFavorite(isFavorite);
                    }
                }
            }
        }

        log.info("根据学科分类id查询学科下的资料列表 mainDataList = {}", mainDataList);

        return mainDataList;
    }

    @Override
    public List<MainData> experienceList() {

        // data_list.html页面中也要是这6个资料id
        List<Integer> experienceDataIdList = CollectionUtil.toList(52, 53, 54, 59, 60 , 61);

        log.info("查询体验页面的6条资料id为：【{}】", experienceDataIdList);
        List<MainData> mainDataList = dataMapper.experienceListByDataIdList(experienceDataIdList);
        log.info("查询体验页面的资料列表为：【{}】", mainDataList);

        return mainDataList;
    }

    @Override
    public String updateMainData(MainData mainData) {

        mainData.setUpdateUser("root");
        mainData.setUpdateTime(new Date());
        mainData.setIsDel(0);
        int update = dataMapper.updateMainData(mainData);
        if (update > 0) {
            log.info("E|DataServiceImpl|updateMainData()|更新文件资料成功！");
            return JsonUtil.toSuccess("操作成功","data-tab",false);
        }

        log.error("E|DataServiceImpl|updateMainData()|更新文件资料失败！");

        return JsonUtil.toError("操作失败");
    }
}
